package com.sgb.knowledgeshare.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.knowledgeshare.R;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import java.util.List;

public class RecommendMenuAdapter extends BaseRecycleAdapter<VideoBean> {

    private int mCurPos;
    private boolean isPlaying = true;

    public RecommendMenuAdapter(Context context, List<VideoBean> datas) {
        super(context, R.layout.item_recommend_menu, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, VideoBean recordsBean) {

    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        //VideoBean recordsBean = getDatas().get(position);
       // holder.setBinding(BR.viewModel, recordsBean);
        ImageView ivCover = holder.itemView.findViewById(R.id.ivCover);
        ImageView ivPlay = holder.itemView.findViewById(R.id.iv_play);
        RequestOptions options = new RequestOptions()
                .bitmapTransform(new RoundedCorners(4)).override(300,300)
                .placeholder(R.mipmap.share_classify_loading)//图片加载出来前，显示的图片
                .fallback(R.mipmap.share_classify_failure) //url为空的时候,显示的图片
                .error(R.mipmap.share_classify_failure);//图片加载失败后，显示的图片
        Glide.with(context).load(datas.get(position).getCover()).apply(options).into(ivCover);
        FrameLayout frameLayout = holder.itemView.findViewById(R.id.image_layout);
        frameLayout.setBackgroundResource(R.color.black);
        ImageView imageView = holder.itemView.findViewById(R.id.iv_picture);
        if (mCurPos == position) {
            imageView.setVisibility(View.GONE);
            ivPlay.setVisibility(View.VISIBLE);
            ivPlay.setBackgroundResource(isPlaying ? R.drawable.jc_pause_normal : R.drawable.jc_play_normal);
        } else {
            imageView.setVisibility(View.VISIBLE);
            ivPlay.setVisibility(View.GONE);
        }

        holder.getmBinding().getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onItemClick(datas.get(position), position);
                }
                mCurPos = position;
                notifyDataSetChanged();
            }
        });
    }

    public void setCurPos(int curPos) {
        mCurPos = curPos;
        isPlaying = true;
        notifyDataSetChanged();
    }

    public void setPlaying(boolean playing) {
        isPlaying = playing;
        notifyItemChanged(mCurPos);
    }

    public int getCurPos() {
        return mCurPos;
    }
}